<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>channelrequest.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_channelrequest.js.html">channelrequest.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2006 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Definition of the ChannelRequest class. The ChannelRequest
<a name="line17"></a> * object encapsulates the logic for making a single request, either for the
<a name="line18"></a> * forward channel, back channel, or test channel, to the server. It contains
<a name="line19"></a> * the logic for the three types of transports we use in the BrowserChannel:
<a name="line20"></a> * XMLHTTP, Trident ActiveX (ie only), and Image request. It provides retry
<a name="line21"></a> * and timeout detection. This class is part of the BrowserChannel
<a name="line22"></a> * implementation and is not for use by normal application code.
<a name="line23"></a> *
<a name="line24"></a> */
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Namespace for BrowserChannel
<a name="line28"></a> */
<a name="line29"></a>goog.provide(&#39;goog.net.ChannelRequest&#39;);
<a name="line30"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line31"></a>goog.require(&#39;goog.Uri&#39;);
<a name="line32"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line33"></a>goog.require(&#39;goog.net.XhrIo&#39;);
<a name="line34"></a>goog.require(&#39;goog.net.XmlHttp&#39;);
<a name="line35"></a>goog.require(&#39;goog.net.tmpnetwork&#39;);
<a name="line36"></a>goog.require(&#39;goog.object&#39;);
<a name="line37"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * Creates a ChannelRequest object which encapsulates a request to the server.
<a name="line41"></a> * A new ChannelRequest is created for each request to the server.
<a name="line42"></a> *
<a name="line43"></a> * @param {goog.net.BrowserChannel|goog.net.BrowserTestChannel} channel
<a name="line44"></a> *     The BrowserChannel that owns this request.
<a name="line45"></a> * @param {goog.net.ChannelDebug} channelDebug A ChannelDebug to use for
<a name="line46"></a> *     logging.
<a name="line47"></a> * @param {string} opt_sessionId  The session id for the channel.
<a name="line48"></a> * @param {string|number} opt_requestId  The request id for this request.
<a name="line49"></a> * @constructor
<a name="line50"></a> */
<a name="line51"></a>goog.net.ChannelRequest = function(
<a name="line52"></a>    channel, channelDebug, opt_sessionId, opt_requestId) {
<a name="line53"></a>  /**
<a name="line54"></a>   * The BrowserChannel object that owns the request.
<a name="line55"></a>   * @type {goog.net.BrowserChannel|goog.net.BrowserTestChannel}
<a name="line56"></a>   * @private
<a name="line57"></a>   */
<a name="line58"></a>  this.channel_ = channel;
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * The channel debug to use for logging
<a name="line62"></a>   * @type {goog.net.ChannelDebug}
<a name="line63"></a>   * @private
<a name="line64"></a>   */
<a name="line65"></a>  this.channelDebug_ = channelDebug;
<a name="line66"></a>
<a name="line67"></a>  /**
<a name="line68"></a>   * The Session ID for the channel.
<a name="line69"></a>   * @type {string|undefined}
<a name="line70"></a>   * @private
<a name="line71"></a>   */
<a name="line72"></a>  this.sid_ = opt_sessionId;
<a name="line73"></a>
<a name="line74"></a> /**
<a name="line75"></a>   * The RID (request ID) for the request.
<a name="line76"></a>   * @type {string|number|undefined}
<a name="line77"></a>   * @private
<a name="line78"></a>   */
<a name="line79"></a>  this.rid_ = opt_requestId;
<a name="line80"></a>
<a name="line81"></a>  /**
<a name="line82"></a>   * The timeout in ms before retrying the request or failing.
<a name="line83"></a>   * @type {number}
<a name="line84"></a>   * @private
<a name="line85"></a>   */
<a name="line86"></a>  this.retryTimeout_ = goog.net.ChannelRequest.TIMEOUT_MS;
<a name="line87"></a>
<a name="line88"></a>  /**
<a name="line89"></a>   * An object to keep track of the channel request event listeners.
<a name="line90"></a>   * @type {!goog.events.EventHandler}
<a name="line91"></a>   * @private
<a name="line92"></a>   */
<a name="line93"></a>  this.eventHandler_ = new goog.events.EventHandler(this);
<a name="line94"></a>
<a name="line95"></a>  /**
<a name="line96"></a>   * A timer for polling responseText in browsers that don&#39;t fire
<a name="line97"></a>   * onreadystatechange during incremental loading of responseText.
<a name="line98"></a>   * @type {goog.Timer}
<a name="line99"></a>   * @private
<a name="line100"></a>   */
<a name="line101"></a>  this.pollingTimer_ = new goog.Timer();
<a name="line102"></a>
<a name="line103"></a>  this.pollingTimer_.setInterval(goog.net.ChannelRequest.POLLING_INTERVAL_MS);
<a name="line104"></a>};
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/**
<a name="line108"></a> * Extra HTTP headers to add to all the requests sent to the server.
<a name="line109"></a> * @type {Object}
<a name="line110"></a> * @private
<a name="line111"></a> */
<a name="line112"></a>goog.net.ChannelRequest.prototype.extraHeaders_ = null;
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * The maximum number of attempts to retry the request before failing.
<a name="line117"></a> * @type {number}
<a name="line118"></a> * @private
<a name="line119"></a> */
<a name="line120"></a>goog.net.ChannelRequest.prototype.maxRetries_ = 0;
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Whether the request was successful. This is only set to true after the
<a name="line125"></a> * request successfuly completes.
<a name="line126"></a> * @type {boolean}
<a name="line127"></a> * @private
<a name="line128"></a> */
<a name="line129"></a>goog.net.ChannelRequest.prototype.successful_ = false;
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * The TimerID of the timer used to detect if the request has timed-out.
<a name="line134"></a> * @type {number?}
<a name="line135"></a> * @private
<a name="line136"></a> */
<a name="line137"></a>goog.net.ChannelRequest.prototype.watchDogTimerId_ = null;
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>/**
<a name="line141"></a> * The time in the future when the request will timeout.
<a name="line142"></a> * @type {number?}
<a name="line143"></a> * @private
<a name="line144"></a> */
<a name="line145"></a>goog.net.ChannelRequest.prototype.watchDogTimeoutTime_ = null;
<a name="line146"></a>
<a name="line147"></a>
<a name="line148"></a>/**
<a name="line149"></a> * The attempt number of the current request.
<a name="line150"></a> * @type {number}
<a name="line151"></a> * @private
<a name="line152"></a> */
<a name="line153"></a>goog.net.ChannelRequest.prototype.retryCount_ = 0;
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/**
<a name="line157"></a> * The type of request (XMLHTTP, IMG, Trident)
<a name="line158"></a> * @type {number?}
<a name="line159"></a> * @private
<a name="line160"></a> */
<a name="line161"></a>goog.net.ChannelRequest.prototype.type_ = null;
<a name="line162"></a>
<a name="line163"></a>
<a name="line164"></a>/**
<a name="line165"></a> * The base Uri for the request. The includes all the parameters except the
<a name="line166"></a> * one that indicates the retry number.
<a name="line167"></a> * @type {goog.Uri?}
<a name="line168"></a> * @private
<a name="line169"></a> */
<a name="line170"></a>goog.net.ChannelRequest.prototype.baseUri_ = null;
<a name="line171"></a>
<a name="line172"></a>
<a name="line173"></a>/**
<a name="line174"></a> * The request Uri that was actually used for the most recent request attempt.
<a name="line175"></a> * @type {goog.Uri?}
<a name="line176"></a> * @private
<a name="line177"></a> */
<a name="line178"></a>goog.net.ChannelRequest.prototype.requestUri_ = null;
<a name="line179"></a>
<a name="line180"></a>
<a name="line181"></a>/**
<a name="line182"></a> * The post data, if the request is a post.
<a name="line183"></a> * @type {string?}
<a name="line184"></a> * @private
<a name="line185"></a> */
<a name="line186"></a>goog.net.ChannelRequest.prototype.postData_ = null;
<a name="line187"></a>
<a name="line188"></a>
<a name="line189"></a>/**
<a name="line190"></a> * The XhrLte request if the request is using XMLHTTP
<a name="line191"></a> * @type {goog.net.XhrIo}
<a name="line192"></a> * @private
<a name="line193"></a> */
<a name="line194"></a>goog.net.ChannelRequest.prototype.xmlHttp_ = null;
<a name="line195"></a>
<a name="line196"></a>
<a name="line197"></a>/**
<a name="line198"></a> * The position of where the next unprocessed chunk starts in the response
<a name="line199"></a> * text.
<a name="line200"></a> * @type {number?}
<a name="line201"></a> * @private
<a name="line202"></a> */
<a name="line203"></a>goog.net.ChannelRequest.prototype.xmlHttpChunkStart_ = null;
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * The Trident instance if the request is using Trident.
<a name="line208"></a> * @type {ActiveXObject?}
<a name="line209"></a> * @private
<a name="line210"></a> */
<a name="line211"></a>goog.net.ChannelRequest.prototype.trident_ = null;
<a name="line212"></a>
<a name="line213"></a>
<a name="line214"></a>/**
<a name="line215"></a> * The verb (Get or Post) for the request.
<a name="line216"></a> * @type {string?}
<a name="line217"></a> * @private
<a name="line218"></a> */
<a name="line219"></a>goog.net.ChannelRequest.prototype.verb_ = null;
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>/**
<a name="line223"></a> * The last error if the request failed.
<a name="line224"></a> * @type {?goog.net.ChannelRequest.Error}
<a name="line225"></a> * @private
<a name="line226"></a> */
<a name="line227"></a>goog.net.ChannelRequest.prototype.lastError_ = null;
<a name="line228"></a>
<a name="line229"></a>
<a name="line230"></a>/**
<a name="line231"></a> * The last status code received.
<a name="line232"></a> * @type {number}
<a name="line233"></a> * @private
<a name="line234"></a> */
<a name="line235"></a>goog.net.ChannelRequest.prototype.lastStatusCode_ = -1;
<a name="line236"></a>
<a name="line237"></a>
<a name="line238"></a>/**
<a name="line239"></a> * Whether to send the Connection:close header as part of the request.
<a name="line240"></a> * @type {boolean}
<a name="line241"></a> * @private
<a name="line242"></a> */
<a name="line243"></a>goog.net.ChannelRequest.prototype.sendClose_ = true;
<a name="line244"></a>
<a name="line245"></a>
<a name="line246"></a>/**
<a name="line247"></a> * Whether the request has been cancelled due to a call to cancel.
<a name="line248"></a> * @type {boolean}
<a name="line249"></a> * @private
<a name="line250"></a> */
<a name="line251"></a>goog.net.ChannelRequest.prototype.cancelled_ = false;
<a name="line252"></a>
<a name="line253"></a>
<a name="line254"></a>/**
<a name="line255"></a> * Default timeout in MS for a request. The server must return data within this
<a name="line256"></a> * time limit for the request to not timeout.
<a name="line257"></a> * @type {number}
<a name="line258"></a> */
<a name="line259"></a>goog.net.ChannelRequest.TIMEOUT_MS = 45 * 1000;
<a name="line260"></a>
<a name="line261"></a>
<a name="line262"></a>/**
<a name="line263"></a> * Default timeout in MS before the initial retry. Subsequent retries will be
<a name="line264"></a> * slower.
<a name="line265"></a> * @type {number}
<a name="line266"></a> */
<a name="line267"></a>goog.net.ChannelRequest.RETRY_DELAY_MS = 5 * 1000;
<a name="line268"></a>
<a name="line269"></a>
<a name="line270"></a>/**
<a name="line271"></a> * We will add a random time between 0 and this number of MS to retries to
<a name="line272"></a> * the retry time for this request.
<a name="line273"></a> * @type {number}
<a name="line274"></a> */
<a name="line275"></a>goog.net.ChannelRequest.RETRY_DELAY_SEED = 10 * 1000;
<a name="line276"></a>
<a name="line277"></a>
<a name="line278"></a>/**
<a name="line279"></a> * When retrying for an inactive channel, we will multiply the total delay by
<a name="line280"></a> * this number.
<a name="line281"></a> * @type {number}
<a name="line282"></a> */
<a name="line283"></a>goog.net.ChannelRequest.INACTIVE_CHANNEL_RETRY_FACTOR = 2;
<a name="line284"></a>
<a name="line285"></a>
<a name="line286"></a>/**
<a name="line287"></a> * How often to poll (in MS) for changes to responseText in browsers that don&#39;t
<a name="line288"></a> * fire onreadystatechange during incremental loading of responseText.
<a name="line289"></a> * @type {number}
<a name="line290"></a> */
<a name="line291"></a>goog.net.ChannelRequest.POLLING_INTERVAL_MS = 250;
<a name="line292"></a>
<a name="line293"></a>
<a name="line294"></a>/**
<a name="line295"></a> * Minimum version of Safari that receives a non-null responseText in ready
<a name="line296"></a> * state interactive.
<a name="line297"></a> * @type {string}
<a name="line298"></a> * @private
<a name="line299"></a> */
<a name="line300"></a>goog.net.ChannelRequest.MIN_WEBKIT_FOR_INTERACTIVE_ = &#39;420+&#39;;
<a name="line301"></a>
<a name="line302"></a>
<a name="line303"></a>/**
<a name="line304"></a> * Enum for channel requests type
<a name="line305"></a> * @enum {number}
<a name="line306"></a> * @private
<a name="line307"></a> */
<a name="line308"></a>goog.net.ChannelRequest.Type_ = {
<a name="line309"></a>  /**
<a name="line310"></a>   * XMLHTTP requests.
<a name="line311"></a>   */
<a name="line312"></a>  XML_HTTP: 1,
<a name="line313"></a>
<a name="line314"></a>  /**
<a name="line315"></a>   * IMG requests.
<a name="line316"></a>   */
<a name="line317"></a>  IMG: 2,
<a name="line318"></a>
<a name="line319"></a>  /**
<a name="line320"></a>   * Requests that use the MSHTML ActiveX control.
<a name="line321"></a>   */
<a name="line322"></a>  TRIDENT: 3
<a name="line323"></a>};
<a name="line324"></a>
<a name="line325"></a>
<a name="line326"></a>/**
<a name="line327"></a> * Enum type for identifying a ChannelRequest error.
<a name="line328"></a> * @enum {number}
<a name="line329"></a> */
<a name="line330"></a>goog.net.ChannelRequest.Error = {
<a name="line331"></a>  /**
<a name="line332"></a>   * Errors due to a non-200 status code.
<a name="line333"></a>   */
<a name="line334"></a>  STATUS: 0,
<a name="line335"></a>
<a name="line336"></a>  /**
<a name="line337"></a>   * Errors due to no data being returned.
<a name="line338"></a>   */
<a name="line339"></a>  NO_DATA: 1,
<a name="line340"></a>
<a name="line341"></a>  /**
<a name="line342"></a>   * Errors due to a timeout.
<a name="line343"></a>   */
<a name="line344"></a>  TIMEOUT: 2,
<a name="line345"></a>
<a name="line346"></a>  /**
<a name="line347"></a>   * Errors due to the server returning an unknown.
<a name="line348"></a>   */
<a name="line349"></a>  UNKNOWN_SESSION_ID: 3,
<a name="line350"></a>
<a name="line351"></a>  /**
<a name="line352"></a>   * Errors due to bad data being received.
<a name="line353"></a>   */
<a name="line354"></a>  BAD_DATA: 4,
<a name="line355"></a>
<a name="line356"></a>  /**
<a name="line357"></a>   * Errors due to the handler throwing an exception.
<a name="line358"></a>   */
<a name="line359"></a>  HANDLER_EXCEPTION: 5
<a name="line360"></a>};
<a name="line361"></a>
<a name="line362"></a>
<a name="line363"></a>/**
<a name="line364"></a> * Sentinel value used to indicate an invalid chunk in a multi-chunk response.
<a name="line365"></a> * @type {Object}
<a name="line366"></a> * @private
<a name="line367"></a> */
<a name="line368"></a>goog.net.ChannelRequest.INVALID_CHUNK_ = {};
<a name="line369"></a>
<a name="line370"></a>
<a name="line371"></a>/**
<a name="line372"></a> * Sentinel value used to indicate an incomplete chunk in a multi-chunk
<a name="line373"></a> * response.
<a name="line374"></a> * @type {Object}
<a name="line375"></a> * @private
<a name="line376"></a> */
<a name="line377"></a>goog.net.ChannelRequest.INCOMPLETE_CHUNK_ = {};
<a name="line378"></a>
<a name="line379"></a>
<a name="line380"></a>/**
<a name="line381"></a> * Sets extra HTTP headers to add to all the requests sent to the server.
<a name="line382"></a> *
<a name="line383"></a> * @param {Object} extraHeaders The HTTP headers.
<a name="line384"></a> */
<a name="line385"></a>goog.net.ChannelRequest.prototype.setExtraHeaders = function(extraHeaders) {
<a name="line386"></a>  this.extraHeaders_ = extraHeaders;
<a name="line387"></a>};
<a name="line388"></a>
<a name="line389"></a>
<a name="line390"></a>/**
<a name="line391"></a> * Sets the maximum number of retries for a request.  If the request is already
<a name="line392"></a> * on a retry beyond the new maxRetries, then it will fail immediately.
<a name="line393"></a> *
<a name="line394"></a> * @param {number} maxRetries The maximum number of retries for a request.
<a name="line395"></a> */
<a name="line396"></a>goog.net.ChannelRequest.prototype.setMaxRetries = function(maxRetries) {
<a name="line397"></a>  this.maxRetries_ = maxRetries;
<a name="line398"></a>  if (this.retryCount_ &gt; maxRetries) {
<a name="line399"></a>    this.channelDebug_.info(
<a name="line400"></a>        &#39;Retry count &#39; + this.retryCount_ +
<a name="line401"></a>        &#39; &gt; new maxRetries &#39; + maxRetries + &#39;. Fail immediately!&#39;);
<a name="line402"></a>
<a name="line403"></a>    // Maybe we&#39;re idly waiting for the next retry.  Cancel that timer.
<a name="line404"></a>    this.cancelRetryTimer_();
<a name="line405"></a>
<a name="line406"></a>    // Maybe the retry is in flight.  Cancel that timer and request.
<a name="line407"></a>    this.cancelWatchDogTimer_();
<a name="line408"></a>    this.cleanup_();
<a name="line409"></a>
<a name="line410"></a>    // Go through the standard maybeRetry logic to expose the max-retry failure
<a name="line411"></a>    // in the standard way.
<a name="line412"></a>    this.maybeRetry_();
<a name="line413"></a>  }
<a name="line414"></a>};
<a name="line415"></a>
<a name="line416"></a>
<a name="line417"></a>/**
<a name="line418"></a> * Sets the retry timeout for a request
<a name="line419"></a> *
<a name="line420"></a> * @param {number} retryTimeout   The timeout in MS for when we either retry the
<a name="line421"></a> *                                request or fail.
<a name="line422"></a> */
<a name="line423"></a>goog.net.ChannelRequest.prototype.setRetryTimeout = function(retryTimeout) {
<a name="line424"></a>  this.retryTimeout_ = retryTimeout;
<a name="line425"></a>};
<a name="line426"></a>
<a name="line427"></a>
<a name="line428"></a>/**
<a name="line429"></a> * Uses XMLHTTP to send an HTTP POST to the server.
<a name="line430"></a> *
<a name="line431"></a> * @param {goog.Uri} uri  The uri of the request.
<a name="line432"></a> * @param {string} postData  The data for the post body.
<a name="line433"></a> * @param {boolean} decodeChunks  Whether to the result is expected to be
<a name="line434"></a> *     encoded for chunking and thus requires decoding.
<a name="line435"></a> */
<a name="line436"></a>goog.net.ChannelRequest.prototype.xmlHttpPost = function(uri, postData,
<a name="line437"></a>                                                         decodeChunks) {
<a name="line438"></a>  this.type_ = goog.net.ChannelRequest.Type_.XML_HTTP;
<a name="line439"></a>  this.baseUri_ = uri.clone().makeUnique();
<a name="line440"></a>  this.postData_ = postData;
<a name="line441"></a>  this.decodeChunks_ = decodeChunks;
<a name="line442"></a>  this.sendXmlHttp_();
<a name="line443"></a>};
<a name="line444"></a>
<a name="line445"></a>
<a name="line446"></a>/**
<a name="line447"></a> * Uses XMLHTTP to send an HTTP GET to the server.
<a name="line448"></a> *
<a name="line449"></a> * @param {goog.Uri} uri  The uri of the request.
<a name="line450"></a> * @param {boolean} decodeChunks  Whether to the result is expected to be
<a name="line451"></a> *     encoded for chunking and thus requires decoding.
<a name="line452"></a> * @param {boolean} opt_noClose   Whether to request that the tcp/ip connection
<a name="line453"></a> *     should be closed.
<a name="line454"></a> */
<a name="line455"></a>goog.net.ChannelRequest.prototype.xmlHttpGet = function(uri, decodeChunks,
<a name="line456"></a>                                                        opt_noClose) {
<a name="line457"></a>  this.type_ = goog.net.ChannelRequest.Type_.XML_HTTP;
<a name="line458"></a>  this.baseUri_ = uri.clone().makeUnique();
<a name="line459"></a>  this.postData_ = null;
<a name="line460"></a>  this.decodeChunks_ = decodeChunks;
<a name="line461"></a>  if (opt_noClose) {
<a name="line462"></a>    this.sendClose_ = false;
<a name="line463"></a>  }
<a name="line464"></a>  this.sendXmlHttp_();
<a name="line465"></a>};
<a name="line466"></a>
<a name="line467"></a>
<a name="line468"></a>/**
<a name="line469"></a> * Sends a request via XMLHTTP according to the current state of the
<a name="line470"></a> * ChannelRequest object.
<a name="line471"></a> *
<a name="line472"></a> * @private
<a name="line473"></a> */
<a name="line474"></a>goog.net.ChannelRequest.prototype.sendXmlHttp_ = function() {
<a name="line475"></a>  // clone the base URI to create the request URI. The request uri has the
<a name="line476"></a>  // attempt number as a parameter which helps in debugging.
<a name="line477"></a>  this.requestUri_ = this.baseUri_.clone();
<a name="line478"></a>  this.requestUri_.setParameterValues(&#39;t&#39;, this.retryCount_ + 1);
<a name="line479"></a>
<a name="line480"></a>  // send the request either as a POST or GET
<a name="line481"></a>  this.xmlHttpChunkStart_ = 0;
<a name="line482"></a>  this.xmlHttp_ = this.createXhrIo_();
<a name="line483"></a>  goog.events.listen(this.xmlHttp_, goog.net.EventType.READY_STATE_CHANGE,
<a name="line484"></a>      this.xmlHttpHandler_, false, this);
<a name="line485"></a>
<a name="line486"></a>  var headers = this.extraHeaders_ ? goog.object.clone(this.extraHeaders_) : {};
<a name="line487"></a>  if (this.postData_) {
<a name="line488"></a>    // todo (jonp) - use POST constant when Dan defines it
<a name="line489"></a>    this.verb_ = &#39;POST&#39;;
<a name="line490"></a>    headers[&#39;Content-Type&#39;] = &#39;application/x-www-form-urlencoded&#39;;
<a name="line491"></a>    this.xmlHttp_.send(this.requestUri_, this.verb_, this.postData_, headers);
<a name="line492"></a>  } else {
<a name="line493"></a>    // todo (jonp) - use GET constant when Dan defines it
<a name="line494"></a>    this.verb_ = &#39;GET&#39;;
<a name="line495"></a>    if (this.sendClose_) {
<a name="line496"></a>      headers[&#39;Connection&#39;] = &#39;close&#39;;
<a name="line497"></a>    }
<a name="line498"></a>    this.xmlHttp_.send(this.requestUri_, this.verb_, null, headers);
<a name="line499"></a>  }
<a name="line500"></a>  this.channelDebug_.xmlHttpChannelRequest(this.verb_,
<a name="line501"></a>      this.requestUri_, this.rid_, this.retryCount_ + 1,
<a name="line502"></a>      this.postData_);
<a name="line503"></a>  this.ensureWatchDogTimer_();
<a name="line504"></a>};
<a name="line505"></a>
<a name="line506"></a>
<a name="line507"></a>/**
<a name="line508"></a> * @return {goog.net.XhrIo} New XhrIo.  Separated out for testing.
<a name="line509"></a> * @private
<a name="line510"></a> */
<a name="line511"></a>goog.net.ChannelRequest.prototype.createXhrIo_ = function() {
<a name="line512"></a>  return new goog.net.XhrIo();
<a name="line513"></a>};
<a name="line514"></a>
<a name="line515"></a>
<a name="line516"></a>/**
<a name="line517"></a> * XmlHttp handler
<a name="line518"></a> * @param {goog.events.Event} e Event object, target is a XhrIo object.
<a name="line519"></a> * @private
<a name="line520"></a> */
<a name="line521"></a>goog.net.ChannelRequest.prototype.xmlHttpHandler_ = function(e) {
<a name="line522"></a>  var xmlhttp = e.target;
<a name="line523"></a>  goog.net.BrowserChannel.onStartExecution();
<a name="line524"></a>  /** @preserveTry */
<a name="line525"></a>  try {
<a name="line526"></a>    if (xmlhttp == this.xmlHttp_) {
<a name="line527"></a>      this.onXmlHttpReadyStateChanged_();
<a name="line528"></a>    } else {
<a name="line529"></a>      this.channelDebug_.warning(&#39;Called back with an &#39; +
<a name="line530"></a>                                              &#39;unexpected xmlhttp&#39;);
<a name="line531"></a>    }
<a name="line532"></a>  } catch (ex) {
<a name="line533"></a>    this.channelDebug_.debug(&#39;Failed call to OnXmlHttpReadyStateChanged_&#39;);
<a name="line534"></a>    if (this.xmlHttp_ &amp;&amp; this.xmlHttp_.getResponseText()) {
<a name="line535"></a>      this.channelDebug_.dumpException(ex,
<a name="line536"></a>          &#39;ResponseText: &#39; + this.xmlHttp_.getResponseText());
<a name="line537"></a>    } else {
<a name="line538"></a>      this.channelDebug_.dumpException(ex, &#39;No response text&#39;);
<a name="line539"></a>    }
<a name="line540"></a>  } finally {
<a name="line541"></a>    goog.net.BrowserChannel.onEndExecution();
<a name="line542"></a>  }
<a name="line543"></a>};
<a name="line544"></a>
<a name="line545"></a>
<a name="line546"></a>/**
<a name="line547"></a> * Called by the readystate handler for XMLHTTP requests.
<a name="line548"></a> *
<a name="line549"></a> * @private
<a name="line550"></a> */
<a name="line551"></a>goog.net.ChannelRequest.prototype.onXmlHttpReadyStateChanged_ = function() {
<a name="line552"></a>  var readyState = this.xmlHttp_.getReadyState();
<a name="line553"></a>  // If it is Safari less than 420+, there is a bug that causes null to be
<a name="line554"></a>  // in the responseText on ready state interactive so we must wait for
<a name="line555"></a>  // ready state complete.
<a name="line556"></a>  if (goog.userAgent.IE || (goog.userAgent.WEBKIT &amp;&amp;
<a name="line557"></a>      !goog.userAgent.isVersion(
<a name="line558"></a>          goog.net.ChannelRequest.MIN_WEBKIT_FOR_INTERACTIVE_))) {
<a name="line559"></a>    if (readyState &lt; goog.net.XmlHttp.ReadyState.COMPLETE) {
<a name="line560"></a>      // not yet ready
<a name="line561"></a>      return;
<a name="line562"></a>    }
<a name="line563"></a>  } else {
<a name="line564"></a>    // we get partial results in non-IE browsers on ready state interactive
<a name="line565"></a>    // We also make sure that getResponseText is not null in interactive mode
<a name="line566"></a>    // before we continue.  However, we don&#39;t do it in Opera because it only
<a name="line567"></a>    // fire readyState == INTERACTIVE once.  We need the following code to poll
<a name="line568"></a>    if (readyState &lt; goog.net.XmlHttp.ReadyState.INTERACTIVE ||
<a name="line569"></a>          readyState == goog.net.XmlHttp.ReadyState.INTERACTIVE &amp;&amp;
<a name="line570"></a>          !goog.userAgent.OPERA &amp;&amp; !this.xmlHttp_.getResponseText()) {
<a name="line571"></a>      // not yet ready
<a name="line572"></a>      return;
<a name="line573"></a>    }
<a name="line574"></a>  }
<a name="line575"></a>
<a name="line576"></a>  // got some data so cancel the watchdog timer
<a name="line577"></a>  this.cancelWatchDogTimer_();
<a name="line578"></a>
<a name="line579"></a>  var status = this.xmlHttp_.getStatus();
<a name="line580"></a>  this.lastStatusCode_ = status;
<a name="line581"></a>  var responseText = this.xmlHttp_.getResponseText();
<a name="line582"></a>  if (!responseText) {
<a name="line583"></a>    this.channelDebug_.debug(&#39;No response text for uri &#39; +
<a name="line584"></a>        this.requestUri_ + &#39; status &#39; + status);
<a name="line585"></a>  }
<a name="line586"></a>  this.successful_ = (status == 200);
<a name="line587"></a>
<a name="line588"></a>  this.channelDebug_.xmlHttpChannelResponseMetaData(
<a name="line589"></a>      /** @type {string} */ (this.verb_),
<a name="line590"></a>      this.requestUri_, this.rid_, this.retryCount_ + 1, readyState,
<a name="line591"></a>      status);
<a name="line592"></a>
<a name="line593"></a>  if (!this.successful_) {
<a name="line594"></a>    if (status == 400 &amp;&amp;
<a name="line595"></a>        responseText.indexOf(&#39;Unknown SID&#39;) &gt; 0) {
<a name="line596"></a>      // the server error string will include &#39;Unknown SID&#39; which indicates the
<a name="line597"></a>      // server doesn&#39;t know about the session (maybe it got restarted, maybe
<a name="line598"></a>      // the user got moved to another server, etc.,). Handlers can special
<a name="line599"></a>      // case this error
<a name="line600"></a>      this.lastError_ = goog.net.ChannelRequest.Error.UNKNOWN_SESSION_ID;
<a name="line601"></a>      goog.net.BrowserChannel.notifyStatEvent(
<a name="line602"></a>          goog.net.BrowserChannel.Stat.REQUEST_UNKNOWN_SESSION_ID);
<a name="line603"></a>    } else {
<a name="line604"></a>      this.lastError_ = goog.net.ChannelRequest.Error.STATUS;
<a name="line605"></a>      goog.net.BrowserChannel.notifyStatEvent(
<a name="line606"></a>          goog.net.BrowserChannel.Stat.REQUEST_BAD_STATUS);
<a name="line607"></a>    }
<a name="line608"></a>    this.channelDebug_.xmlHttpChannelResponseText(this.rid_, responseText);
<a name="line609"></a>    this.cleanup_();
<a name="line610"></a>    this.maybeRetry_();
<a name="line611"></a>    return;
<a name="line612"></a>  }
<a name="line613"></a>
<a name="line614"></a>  if (readyState == goog.net.XmlHttp.ReadyState.COMPLETE) {
<a name="line615"></a>    this.cleanup_();
<a name="line616"></a>  }
<a name="line617"></a>
<a name="line618"></a>  if (this.decodeChunks_) {
<a name="line619"></a>    this.decodeNextChunks_(readyState, responseText);
<a name="line620"></a>    if (goog.userAgent.OPERA &amp;&amp;
<a name="line621"></a>        readyState == goog.net.XmlHttp.ReadyState.INTERACTIVE) {
<a name="line622"></a>      this.startPolling_();
<a name="line623"></a>    }
<a name="line624"></a>  } else {
<a name="line625"></a>    this.channelDebug_.xmlHttpChannelResponseText(
<a name="line626"></a>        this.rid_, responseText, null);
<a name="line627"></a>    this.safeOnRequestData_(responseText);
<a name="line628"></a>  }
<a name="line629"></a>
<a name="line630"></a>  if (!this.successful_) {
<a name="line631"></a>    return;
<a name="line632"></a>  }
<a name="line633"></a>
<a name="line634"></a>  if (!this.cancelled_) {
<a name="line635"></a>    if (readyState == goog.net.XmlHttp.ReadyState.COMPLETE) {
<a name="line636"></a>      this.channel_.onRequestComplete(this);
<a name="line637"></a>    } else {
<a name="line638"></a>      // The default is false, the result from this callback shouldn&#39;t carry
<a name="line639"></a>      // over to the next callback, otherwise the request looks successful if
<a name="line640"></a>      // the watchdog timer gets called
<a name="line641"></a>      this.successful_ = false;
<a name="line642"></a>      this.ensureWatchDogTimer_();
<a name="line643"></a>    }
<a name="line644"></a>  }
<a name="line645"></a>};
<a name="line646"></a>
<a name="line647"></a>
<a name="line648"></a>/**
<a name="line649"></a> * Decodes the next set of available chunks in the response.
<a name="line650"></a> * @param {number} readyState The value of readyState.
<a name="line651"></a> * @param {string} responseText The value of responseText.
<a name="line652"></a> * @private
<a name="line653"></a> */
<a name="line654"></a>goog.net.ChannelRequest.prototype.decodeNextChunks_ = function(readyState,
<a name="line655"></a>        responseText) {
<a name="line656"></a>  var decodeNextChunksSuccessful = true;
<a name="line657"></a>  while (!this.cancelled_ &amp;&amp;
<a name="line658"></a>         this.xmlHttpChunkStart_ &lt; responseText.length) {
<a name="line659"></a>    var chunkText = this.getNextChunk_(responseText);
<a name="line660"></a>    if (chunkText == goog.net.ChannelRequest.INCOMPLETE_CHUNK_) {
<a name="line661"></a>      if (readyState == goog.net.XmlHttp.ReadyState.COMPLETE) {
<a name="line662"></a>        // should have consumed entire response when the request is done
<a name="line663"></a>        this.lastError_ = goog.net.ChannelRequest.Error.BAD_DATA;
<a name="line664"></a>        goog.net.BrowserChannel.notifyStatEvent(
<a name="line665"></a>            goog.net.BrowserChannel.Stat.REQUEST_INCOMPLETE_DATA);
<a name="line666"></a>        decodeNextChunksSuccessful = false;
<a name="line667"></a>      }
<a name="line668"></a>      this.channelDebug_.xmlHttpChannelResponseText(
<a name="line669"></a>          this.rid_, null, &#39;[Incomplete Response]&#39;);
<a name="line670"></a>      break;
<a name="line671"></a>    } else if (chunkText == goog.net.ChannelRequest.INVALID_CHUNK_) {
<a name="line672"></a>      this.lastError_ = goog.net.ChannelRequest.Error.BAD_DATA;
<a name="line673"></a>      goog.net.BrowserChannel.notifyStatEvent(
<a name="line674"></a>          goog.net.BrowserChannel.Stat.REQUEST_BAD_DATA);
<a name="line675"></a>      this.channelDebug_.xmlHttpChannelResponseText(
<a name="line676"></a>          this.rid_, responseText, &#39;[Invalid Chunk]&#39;);
<a name="line677"></a>      decodeNextChunksSuccessful = false;
<a name="line678"></a>      break;
<a name="line679"></a>    } else {
<a name="line680"></a>      this.channelDebug_.xmlHttpChannelResponseText(
<a name="line681"></a>          this.rid_, /** @type {string} */ (chunkText), null);
<a name="line682"></a>      this.safeOnRequestData_(/** @type {string} */ (chunkText));
<a name="line683"></a>    }
<a name="line684"></a>  }
<a name="line685"></a>  if (readyState == goog.net.XmlHttp.ReadyState.COMPLETE &amp;&amp;
<a name="line686"></a>      responseText.length == 0) {
<a name="line687"></a>    // also an error if we didn&#39;t get any response
<a name="line688"></a>    this.lastError_ = goog.net.ChannelRequest.Error.NO_DATA;
<a name="line689"></a>    goog.net.BrowserChannel.notifyStatEvent(
<a name="line690"></a>        goog.net.BrowserChannel.Stat.REQUEST_NO_DATA);
<a name="line691"></a>    this.decodeNextChunksSuccessful = false;
<a name="line692"></a>  }
<a name="line693"></a>  if (!decodeNextChunksSuccessful) {
<a name="line694"></a>    // we can get set back to unsuccessful if the response
<a name="line695"></a>    // wasn&#39;t parsed correctly - we make this trigger retry logic
<a name="line696"></a>    this.channelDebug_.xmlHttpChannelResponseText(
<a name="line697"></a>        this.rid_, responseText, &#39;[Invalid Chunked Response]&#39;);
<a name="line698"></a>    this.cleanup_();
<a name="line699"></a>    this.maybeRetry_();
<a name="line700"></a>  }
<a name="line701"></a>  this.successful_ = this.successful_ &amp;&amp; decodeNextChunksSuccessful;
<a name="line702"></a>};
<a name="line703"></a>
<a name="line704"></a>
<a name="line705"></a>/**
<a name="line706"></a> * Polls the response for new data.
<a name="line707"></a> * @private
<a name="line708"></a> */
<a name="line709"></a>goog.net.ChannelRequest.prototype.pollResponse_ = function() {
<a name="line710"></a>  var readyState = this.xmlHttp_.getReadyState();
<a name="line711"></a>  var responseText = this.xmlHttp_.getResponseText();
<a name="line712"></a>  if (this.xmlHttpChunkStart_ &lt; responseText.length) {
<a name="line713"></a>    this.cancelWatchDogTimer_();
<a name="line714"></a>    this.decodeNextChunks_(readyState, responseText);
<a name="line715"></a>    if (this.successful_ &amp;&amp;
<a name="line716"></a>        readyState != goog.net.XmlHttp.ReadyState.COMPLETE) {
<a name="line717"></a>      this.ensureWatchDogTimer_();
<a name="line718"></a>    }
<a name="line719"></a>  }
<a name="line720"></a>};
<a name="line721"></a>
<a name="line722"></a>
<a name="line723"></a>/**
<a name="line724"></a> * Starts a polling interval for changes to responseText of the
<a name="line725"></a> * XMLHttpRequest, for browsers that don&#39;t fire onreadystatechange
<a name="line726"></a> * as data comes in incrementally.
<a name="line727"></a> * @private
<a name="line728"></a> */
<a name="line729"></a>goog.net.ChannelRequest.prototype.startPolling_ = function() {
<a name="line730"></a>  this.eventHandler_.listen(this.pollingTimer_, goog.Timer.TICK,
<a name="line731"></a>          this.pollResponse_);
<a name="line732"></a>  this.pollingTimer_.start();
<a name="line733"></a>};
<a name="line734"></a>
<a name="line735"></a>
<a name="line736"></a>/**
<a name="line737"></a> * Stops the polling interval for changes to responseText.
<a name="line738"></a> * @private
<a name="line739"></a> */
<a name="line740"></a>goog.net.ChannelRequest.prototype.stopPolling_ = function() {
<a name="line741"></a>  this.pollingTimer_.stop();
<a name="line742"></a>  this.eventHandler_.removeAll();
<a name="line743"></a>};
<a name="line744"></a>
<a name="line745"></a>
<a name="line746"></a>/**
<a name="line747"></a> * Returns the next chunk of a chunk-encoded response. This is not standard
<a name="line748"></a> * HTTP chunked encoding because browsers don&#39;t expose the chunk boundaries to
<a name="line749"></a> * the application through XMLHTTP. So we have an additional chunk encoding at
<a name="line750"></a> * the application level that lets us tell where the beginning and end of
<a name="line751"></a> * individual responses are so that we can only try to eval a complete JS array.
<a name="line752"></a> *
<a name="line753"></a> * The encoding is the size of the chunk encoded as a decimal string followed
<a name="line754"></a> * by a newline followed by the data.
<a name="line755"></a> *
<a name="line756"></a> * @param {string} responseText The response text from the XMLHTTP response.
<a name="line757"></a> * @return {string|Object} The next chunk string or a sentinel object
<a name="line758"></a> *                         indicating a special condition.
<a name="line759"></a> * @private
<a name="line760"></a> */
<a name="line761"></a>goog.net.ChannelRequest.prototype.getNextChunk_ = function(responseText) {
<a name="line762"></a>  var sizeStartIndex = this.xmlHttpChunkStart_;
<a name="line763"></a>  var sizeEndIndex = responseText.indexOf(&#39;\n&#39;, sizeStartIndex);
<a name="line764"></a>  if (sizeEndIndex == -1) {
<a name="line765"></a>    return goog.net.ChannelRequest.INCOMPLETE_CHUNK_;
<a name="line766"></a>  }
<a name="line767"></a>
<a name="line768"></a>  var sizeAsString = responseText.substring(sizeStartIndex, sizeEndIndex);
<a name="line769"></a>  var size = Number(sizeAsString);
<a name="line770"></a>  if (isNaN(size)) {
<a name="line771"></a>    return goog.net.ChannelRequest.INVALID_CHUNK_;
<a name="line772"></a>  }
<a name="line773"></a>
<a name="line774"></a>  var chunkStartIndex = sizeEndIndex + 1;
<a name="line775"></a>  if (chunkStartIndex + size &gt; responseText.length) {
<a name="line776"></a>    return goog.net.ChannelRequest.INCOMPLETE_CHUNK_;
<a name="line777"></a>  }
<a name="line778"></a>
<a name="line779"></a>  var chunkText = responseText.substr(chunkStartIndex, size);
<a name="line780"></a>  this.xmlHttpChunkStart_ = chunkStartIndex + size;
<a name="line781"></a>  return chunkText;
<a name="line782"></a>};
<a name="line783"></a>
<a name="line784"></a>
<a name="line785"></a>/**
<a name="line786"></a> * Uses the Trident htmlfile ActiveX control to send a GET request in IE. This
<a name="line787"></a> * is the innovation discovered that lets us get intermediate results in
<a name="line788"></a> * Internet Explorer.  Thanks to http://go/kev
<a name="line789"></a> * @param {goog.Uri} uri The uri to request from.
<a name="line790"></a> * @param {boolean} usingSecondaryDomain Whether to use a secondary domain.
<a name="line791"></a> */
<a name="line792"></a>goog.net.ChannelRequest.prototype.tridentGet = function(uri,
<a name="line793"></a>    usingSecondaryDomain) {
<a name="line794"></a>  this.type_ = goog.net.ChannelRequest.Type_.TRIDENT;
<a name="line795"></a>  this.baseUri_ = uri.clone().makeUnique();
<a name="line796"></a>  this.tridentGet_(usingSecondaryDomain);
<a name="line797"></a>};
<a name="line798"></a>
<a name="line799"></a>
<a name="line800"></a>/**
<a name="line801"></a> * Starts the Trident request.
<a name="line802"></a> * @param {boolean} usingSecondaryDomain Whether to use a secondary domain.
<a name="line803"></a> * @private
<a name="line804"></a> */
<a name="line805"></a>goog.net.ChannelRequest.prototype.tridentGet_ = function(usingSecondaryDomain) {
<a name="line806"></a>  this.trident_ = new ActiveXObject(&#39;htmlfile&#39;);
<a name="line807"></a>
<a name="line808"></a>  var hostname = &#39;&#39;;
<a name="line809"></a>  var body = &#39;&lt;html&gt;&#39;;
<a name="line810"></a>  if (usingSecondaryDomain) {
<a name="line811"></a>    hostname = window.location.hostname;
<a name="line812"></a>    body += &#39;&lt;script&gt;document.domain=&quot;&#39; + hostname + &#39;&quot;&lt;/scr&#39; + &#39;ipt&gt;&#39;;
<a name="line813"></a>  }
<a name="line814"></a>  body += &#39;&lt;/html&gt;&#39;;
<a name="line815"></a>
<a name="line816"></a>  this.trident_.open();
<a name="line817"></a>  this.trident_.write(body);
<a name="line818"></a>  this.trident_.close();
<a name="line819"></a>
<a name="line820"></a>  this.trident_.parentWindow[&#39;m&#39;] = goog.bind(this.onTridentRpcMessage_, this);
<a name="line821"></a>  this.trident_.parentWindow[&#39;d&#39;] = goog.bind(this.onTridentDone_, this, true);
<a name="line822"></a>  this.trident_.parentWindow[&#39;rpcClose&#39;] =
<a name="line823"></a>      goog.bind(this.onTridentDone_, this, false);
<a name="line824"></a>
<a name="line825"></a>  var div = this.trident_.createElement(&#39;div&#39;);
<a name="line826"></a>  this.trident_.appendChild(div);
<a name="line827"></a>  this.requestUri_ = this.baseUri_.clone();
<a name="line828"></a>  this.requestUri_.setParameterValue(&#39;DOMAIN&#39;, hostname);
<a name="line829"></a>  this.requestUri_.setParameterValue(&#39;t&#39;, this.retryCount_ + 1);
<a name="line830"></a>  div.innerHTML = &#39;&lt;iframe src=&quot;&#39; + this.requestUri_ + &#39;&quot;&gt;&lt;/iframe&gt;&#39;;
<a name="line831"></a>
<a name="line832"></a>  this.channelDebug_.tridentChannelRequest(&#39;GET&#39;,
<a name="line833"></a>      this.requestUri_, this.rid_, this.retryCount_ + 1);
<a name="line834"></a>
<a name="line835"></a>  this.ensureWatchDogTimer_();
<a name="line836"></a>};
<a name="line837"></a>
<a name="line838"></a>
<a name="line839"></a>/**
<a name="line840"></a> * Callback from the Trident htmlfile ActiveX control for when a new message
<a name="line841"></a> * is received.
<a name="line842"></a> *
<a name="line843"></a> * @param {string} msg The data payload.
<a name="line844"></a> * @private
<a name="line845"></a> */
<a name="line846"></a>goog.net.ChannelRequest.prototype.onTridentRpcMessage_ = function(msg) {
<a name="line847"></a>  // need to do async b/c this gets called off of the context of the ActiveX
<a name="line848"></a>  goog.net.BrowserChannel.setTimeout(
<a name="line849"></a>      goog.bind(this.onTridentRpcMessageAsync_, this, msg), 0);
<a name="line850"></a>};
<a name="line851"></a>
<a name="line852"></a>
<a name="line853"></a>/**
<a name="line854"></a> * Callback from the Trident htmlfile ActiveX control for when a new message
<a name="line855"></a> * is received.
<a name="line856"></a> *
<a name="line857"></a> * @param {string} msg  The data payload.
<a name="line858"></a> * @private
<a name="line859"></a> */
<a name="line860"></a>goog.net.ChannelRequest.prototype.onTridentRpcMessageAsync_ = function(msg) {
<a name="line861"></a>  if (this.cancelled_) {
<a name="line862"></a>    return;
<a name="line863"></a>  }
<a name="line864"></a>  this.channelDebug_.tridentChannelResponseText(this.rid_, msg);
<a name="line865"></a>  this.cancelWatchDogTimer_();
<a name="line866"></a>  this.safeOnRequestData_(msg);
<a name="line867"></a>  this.ensureWatchDogTimer_();
<a name="line868"></a>};
<a name="line869"></a>
<a name="line870"></a>
<a name="line871"></a>/**
<a name="line872"></a> * Callback from the Trident htmlfile ActiveX control for when the request
<a name="line873"></a> * is complete
<a name="line874"></a> *
<a name="line875"></a> * @param {boolean} successful Whether the request successfully completed.
<a name="line876"></a> * @private
<a name="line877"></a> */
<a name="line878"></a>goog.net.ChannelRequest.prototype.onTridentDone_ = function(successful) {
<a name="line879"></a>  // need to do async b/c this gets called off of the context of the ActiveX
<a name="line880"></a>  goog.net.BrowserChannel.setTimeout(
<a name="line881"></a>      goog.bind(this.onTridentDoneAsync_, this, successful), 0);
<a name="line882"></a>};
<a name="line883"></a>
<a name="line884"></a>
<a name="line885"></a>/**
<a name="line886"></a> * Callback from the Trident htmlfile ActiveX control for when the request
<a name="line887"></a> * is complete
<a name="line888"></a> *
<a name="line889"></a> * @param {boolean} successful Whether the request successfully completed.
<a name="line890"></a> * @private
<a name="line891"></a> */
<a name="line892"></a>goog.net.ChannelRequest.prototype.onTridentDoneAsync_ = function(successful) {
<a name="line893"></a>  if (this.cancelled_) {
<a name="line894"></a>    return;
<a name="line895"></a>  }
<a name="line896"></a>  this.channelDebug_.tridentChannelResponseDone(
<a name="line897"></a>      this.rid_, successful);
<a name="line898"></a>  this.cancelWatchDogTimer_();
<a name="line899"></a>  this.cleanup_();
<a name="line900"></a>  this.successful_ = successful;
<a name="line901"></a>  this.channel_.onRequestComplete(this);
<a name="line902"></a>};
<a name="line903"></a>
<a name="line904"></a>
<a name="line905"></a>/**
<a name="line906"></a> * Uses an IMG tag to send an HTTP get to the server. This is only currently
<a name="line907"></a> * used to terminate the connection, as an IMG tag is the most reliable way to
<a name="line908"></a> * send something to the server while the page is getting torn down.
<a name="line909"></a> * @param {goog.Uri} uri The uri to send a request to.
<a name="line910"></a> */
<a name="line911"></a>goog.net.ChannelRequest.prototype.sendUsingImgTag = function(uri) {
<a name="line912"></a>  this.type_ = goog.net.ChannelRequest.Type_.IMG;
<a name="line913"></a>  this.baseUri_ = uri.clone().makeUnique();
<a name="line914"></a>  this.imgTagGet_();
<a name="line915"></a>};
<a name="line916"></a>
<a name="line917"></a>
<a name="line918"></a>/**
<a name="line919"></a> * Starts the IMG request.
<a name="line920"></a> *
<a name="line921"></a> * @private
<a name="line922"></a> */
<a name="line923"></a>goog.net.ChannelRequest.prototype.imgTagGet_ = function() {
<a name="line924"></a>  var eltImg = new Image();
<a name="line925"></a>  eltImg.src = this.baseUri_;
<a name="line926"></a>  this.ensureWatchDogTimer_();
<a name="line927"></a>};
<a name="line928"></a>
<a name="line929"></a>
<a name="line930"></a>/**
<a name="line931"></a> * Cancels the request no matter what the underlying transport is.
<a name="line932"></a> */
<a name="line933"></a>goog.net.ChannelRequest.prototype.cancel = function() {
<a name="line934"></a>  this.cancelled_ = true;
<a name="line935"></a>  this.cancelWatchDogTimer_();
<a name="line936"></a>  this.cleanup_();
<a name="line937"></a>};
<a name="line938"></a>
<a name="line939"></a>
<a name="line940"></a>/**
<a name="line941"></a> * Ensures that there is watchdog timeout which is used to ensure that
<a name="line942"></a> * the connection completes in time.
<a name="line943"></a> *
<a name="line944"></a> * @private
<a name="line945"></a> */
<a name="line946"></a>goog.net.ChannelRequest.prototype.ensureWatchDogTimer_ = function() {
<a name="line947"></a>  this.watchDogTimeoutTime_ = goog.now() + this.retryTimeout_;
<a name="line948"></a>  this.startWatchDogTimer_(this.retryTimeout_);
<a name="line949"></a>};
<a name="line950"></a>
<a name="line951"></a>
<a name="line952"></a>/**
<a name="line953"></a> * Starts the watchdog timer which is used to ensure that the connection
<a name="line954"></a> * completes in time.
<a name="line955"></a> * @param {number} time The number of milliseconds to wait.
<a name="line956"></a> * @private
<a name="line957"></a> */
<a name="line958"></a>goog.net.ChannelRequest.prototype.startWatchDogTimer_ = function(time) {
<a name="line959"></a>  if (this.watchDogTimerId_ != null) {
<a name="line960"></a>    // assertion
<a name="line961"></a>    throw Error(&#39;WatchDog timer not null&#39;);
<a name="line962"></a>  }
<a name="line963"></a>  this.watchDogTimerId_ = goog.net.BrowserChannel.setTimeout(
<a name="line964"></a>      goog.bind(this.onWatchDogTimout_, this), time);
<a name="line965"></a>};
<a name="line966"></a>
<a name="line967"></a>
<a name="line968"></a>/**
<a name="line969"></a> * Cancels the watchdog timer if it has been started.
<a name="line970"></a> *
<a name="line971"></a> * @private
<a name="line972"></a> */
<a name="line973"></a>goog.net.ChannelRequest.prototype.cancelWatchDogTimer_ = function() {
<a name="line974"></a>  if (this.watchDogTimerId_) {
<a name="line975"></a>    goog.global.clearTimeout(this.watchDogTimerId_);
<a name="line976"></a>    this.watchDogTimerId_ = null;
<a name="line977"></a>  }
<a name="line978"></a>};
<a name="line979"></a>
<a name="line980"></a>
<a name="line981"></a>/**
<a name="line982"></a> * Called when the watchdog timer is triggered. It also handles a case where it
<a name="line983"></a> * is called too early which we suspect may be happening sometimes
<a name="line984"></a> * (not sure why)
<a name="line985"></a> *
<a name="line986"></a> * @private
<a name="line987"></a> */
<a name="line988"></a>goog.net.ChannelRequest.prototype.onWatchDogTimout_ = function() {
<a name="line989"></a>  this.watchDogTimerId_ = null;
<a name="line990"></a>  var now = goog.now();
<a name="line991"></a>  if (now - this.watchDogTimeoutTime_ &gt;= 0) {
<a name="line992"></a>    this.handleTimeout_();
<a name="line993"></a>  } else {
<a name="line994"></a>    // got called too early for some reason
<a name="line995"></a>    this.channelDebug_.warning(&#39;WatchDog timer called too early&#39;);
<a name="line996"></a>    this.startWatchDogTimer_(this.watchDogTimeoutTime_ - now);
<a name="line997"></a>  }
<a name="line998"></a>};
<a name="line999"></a>
<a name="line1000"></a>
<a name="line1001"></a>/**
<a name="line1002"></a> * Called when the request has actually timed out. Will cleanup and maybe retry
<a name="line1003"></a> * the request if we haven&#39;t exceeded the retry count.
<a name="line1004"></a> *
<a name="line1005"></a> * @private
<a name="line1006"></a> */
<a name="line1007"></a>goog.net.ChannelRequest.prototype.handleTimeout_ = function() {
<a name="line1008"></a>  if (this.successful_) {
<a name="line1009"></a>    this.channelDebug_.debug(
<a name="line1010"></a>        &#39;Received watchdog timeout even though request loaded successfully&#39;);
<a name="line1011"></a>  }
<a name="line1012"></a>
<a name="line1013"></a>  this.channelDebug_.timeoutResponse(this.requestUri_);
<a name="line1014"></a>  this.cleanup_();
<a name="line1015"></a>
<a name="line1016"></a>  // set error and maybe retry
<a name="line1017"></a>  this.lastError_ = goog.net.ChannelRequest.Error.TIMEOUT;
<a name="line1018"></a>  goog.net.BrowserChannel.notifyStatEvent(
<a name="line1019"></a>      goog.net.BrowserChannel.Stat.REQUEST_TIMEOUT);
<a name="line1020"></a>  this.maybeRetry_();
<a name="line1021"></a>};
<a name="line1022"></a>
<a name="line1023"></a>
<a name="line1024"></a>/**
<a name="line1025"></a> * Retries the current request if we still meet the conditions for retrying.
<a name="line1026"></a> * @private
<a name="line1027"></a> */
<a name="line1028"></a>goog.net.ChannelRequest.prototype.maybeRetry_ = function() {
<a name="line1029"></a>  if (this.channel_.isClosed() || this.cancelled_) {
<a name="line1030"></a>    return;
<a name="line1031"></a>  }
<a name="line1032"></a>
<a name="line1033"></a>  this.channelDebug_.debug(&#39;Maybe retrying, last error: &#39; +
<a name="line1034"></a>        this.errorStringFromCode_(
<a name="line1035"></a>            /** @type {goog.net.ChannelRequest.Error} */ (this.lastError_)));
<a name="line1036"></a>
<a name="line1037"></a>  if (this.lastError_ == goog.net.ChannelRequest.Error.UNKNOWN_SESSION_ID ||
<a name="line1038"></a>      (this.lastError_ == goog.net.ChannelRequest.Error.STATUS &amp;&amp;
<a name="line1039"></a>       this.lastStatusCode_ &gt; 0)) {
<a name="line1040"></a>    this.channelDebug_.debug(&#39;Not retrying due to error type&#39;);
<a name="line1041"></a>    this.channel_.onRequestComplete(this);
<a name="line1042"></a>  } else if (this.retryCount_ &gt;= this.maxRetries_) {
<a name="line1043"></a>    this.channelDebug_.debug(&#39;Exceeded max number of retries&#39;);
<a name="line1044"></a>    this.channel_.onRequestComplete(this);
<a name="line1045"></a>  } else {
<a name="line1046"></a>    this.channelDebug_.debug(&#39;Going to retry&#39;);
<a name="line1047"></a>    this.retryTimerId_ = goog.net.BrowserChannel.setTimeout(
<a name="line1048"></a>        goog.bind(this.retry_, this), this.getRetryTime_());
<a name="line1049"></a>    this.retryCount_++;
<a name="line1050"></a>  }
<a name="line1051"></a>};
<a name="line1052"></a>
<a name="line1053"></a>
<a name="line1054"></a>/**
<a name="line1055"></a> * @return {number} Time in ms before firing next retry request.
<a name="line1056"></a> * @private
<a name="line1057"></a> */
<a name="line1058"></a>goog.net.ChannelRequest.prototype.getRetryTime_ = function() {
<a name="line1059"></a>  var retryTime = goog.net.ChannelRequest.RETRY_DELAY_MS +
<a name="line1060"></a>      Math.floor(Math.random() * goog.net.ChannelRequest.RETRY_DELAY_SEED);
<a name="line1061"></a>  if (!this.channel_.isActive()) {
<a name="line1062"></a>    this.channelDebug_.debug(&#39;Inactive channel&#39;);
<a name="line1063"></a>    retryTime =
<a name="line1064"></a>        retryTime * goog.net.ChannelRequest.INACTIVE_CHANNEL_RETRY_FACTOR;
<a name="line1065"></a>  }
<a name="line1066"></a>  // Backoff for subsequent retries
<a name="line1067"></a>  retryTime = retryTime * this.retryCount_ + 1;
<a name="line1068"></a>  return retryTime;
<a name="line1069"></a>};
<a name="line1070"></a>
<a name="line1071"></a>
<a name="line1072"></a>/**
<a name="line1073"></a> * Performs a retry by restarting the request
<a name="line1074"></a> * @private
<a name="line1075"></a> */
<a name="line1076"></a>goog.net.ChannelRequest.prototype.retry_ = function() {
<a name="line1077"></a>  this.retryTimerId_ = null;
<a name="line1078"></a>  if (this.type_ == goog.net.ChannelRequest.Type_.XML_HTTP) {
<a name="line1079"></a>    this.sendXmlHttp_();
<a name="line1080"></a>  }
<a name="line1081"></a>};
<a name="line1082"></a>
<a name="line1083"></a>
<a name="line1084"></a>/**
<a name="line1085"></a> * Cancels the retry timer if it has been started.
<a name="line1086"></a> * @private
<a name="line1087"></a> */
<a name="line1088"></a>goog.net.ChannelRequest.prototype.cancelRetryTimer_ = function() {
<a name="line1089"></a>  if (this.retryTimerId_) {
<a name="line1090"></a>    goog.global.clearTimeout(this.retryTimerId_);
<a name="line1091"></a>    this.retryTimerId_ = null;
<a name="line1092"></a>  }
<a name="line1093"></a>};
<a name="line1094"></a>
<a name="line1095"></a>
<a name="line1096"></a>/**
<a name="line1097"></a> * Returns a useful error string for debugging based on the specified error
<a name="line1098"></a> * code.
<a name="line1099"></a> * @param {goog.net.ChannelRequest.Error} errorCode The error code.
<a name="line1100"></a> * @return {string} The error string for the given code.
<a name="line1101"></a> * @private
<a name="line1102"></a> */
<a name="line1103"></a>goog.net.ChannelRequest.prototype.errorStringFromCode_ = function(errorCode) {
<a name="line1104"></a>  switch (errorCode) {
<a name="line1105"></a>    case goog.net.ChannelRequest.Error.STATUS:
<a name="line1106"></a>      return &#39;Non-200 return code (&#39; + this.lastStatusCode_ + &#39;)&#39;;
<a name="line1107"></a>    case goog.net.ChannelRequest.Error.NO_DATA:
<a name="line1108"></a>      return &#39;XMLHTTP failure (no data)&#39;;
<a name="line1109"></a>   case goog.net.ChannelRequest.Error.TIMEOUT:
<a name="line1110"></a>      return &#39;HttpConnection timeout&#39;;
<a name="line1111"></a>    default:
<a name="line1112"></a>      return &#39;Unknown error&#39;;
<a name="line1113"></a>  }
<a name="line1114"></a>};
<a name="line1115"></a>
<a name="line1116"></a>
<a name="line1117"></a>/**
<a name="line1118"></a> * Cleans up the objects used to make the request. This function is
<a name="line1119"></a> * idempotent.
<a name="line1120"></a> *
<a name="line1121"></a> * @private
<a name="line1122"></a> */
<a name="line1123"></a>goog.net.ChannelRequest.prototype.cleanup_ = function() {
<a name="line1124"></a>  this.stopPolling_();
<a name="line1125"></a>  if (this.xmlHttp_) {
<a name="line1126"></a>    // clear out this.xmlHttp_ before aborting so we handle getting reentered
<a name="line1127"></a>    // inside abort
<a name="line1128"></a>    var xmlhttp = this.xmlHttp_;
<a name="line1129"></a>    this.xmlHttp_ = null;
<a name="line1130"></a>    goog.events.unlisten(xmlhttp, goog.net.EventType.READY_STATE_CHANGE,
<a name="line1131"></a>        this.xmlHttpHandler_, false, this);
<a name="line1132"></a>    xmlhttp.abort();
<a name="line1133"></a>  }
<a name="line1134"></a>
<a name="line1135"></a>  if (this.trident_) {
<a name="line1136"></a>    this.trident_ = null;
<a name="line1137"></a>  }
<a name="line1138"></a>};
<a name="line1139"></a>
<a name="line1140"></a>
<a name="line1141"></a>/**
<a name="line1142"></a> * Indicates whether the request was successful. Only valid after the handler
<a name="line1143"></a> * is called to indicate completion of the request.
<a name="line1144"></a> *
<a name="line1145"></a> * @return {boolean} True if the request succeeded.
<a name="line1146"></a> */
<a name="line1147"></a>goog.net.ChannelRequest.prototype.getSuccess = function() {
<a name="line1148"></a>  return this.successful_;
<a name="line1149"></a>};
<a name="line1150"></a>
<a name="line1151"></a>
<a name="line1152"></a>/**
<a name="line1153"></a> * If the request was not successful, returns the reason.
<a name="line1154"></a> *
<a name="line1155"></a> * @return {?goog.net.ChannelRequest.Error}  The last error.
<a name="line1156"></a> */
<a name="line1157"></a>goog.net.ChannelRequest.prototype.getLastError = function() {
<a name="line1158"></a>  return this.lastError_;
<a name="line1159"></a>};
<a name="line1160"></a>
<a name="line1161"></a>/**
<a name="line1162"></a> * Returns the status code of the last request.
<a name="line1163"></a> * @return {number} The status code of the last request.
<a name="line1164"></a> */
<a name="line1165"></a>goog.net.ChannelRequest.prototype.getLastStatusCode = function() {
<a name="line1166"></a>  return this.lastStatusCode_;
<a name="line1167"></a>};
<a name="line1168"></a>
<a name="line1169"></a>/**
<a name="line1170"></a> * Returns the session id for this channel.
<a name="line1171"></a> *
<a name="line1172"></a> * @return {string|undefined} The session ID.
<a name="line1173"></a> */
<a name="line1174"></a>goog.net.ChannelRequest.prototype.getSessionId = function() {
<a name="line1175"></a>  return this.sid_;
<a name="line1176"></a>};
<a name="line1177"></a>
<a name="line1178"></a>/**
<a name="line1179"></a> * Returns the request id for this request. Each request has a unique request
<a name="line1180"></a> * id and the request IDs are a sequential increasing count.
<a name="line1181"></a> *
<a name="line1182"></a> * @return {string|number|undefined} The request ID.
<a name="line1183"></a> */
<a name="line1184"></a>goog.net.ChannelRequest.prototype.getRequestId = function() {
<a name="line1185"></a>  return this.rid_;
<a name="line1186"></a>};
<a name="line1187"></a>
<a name="line1188"></a>
<a name="line1189"></a>/**
<a name="line1190"></a> * Helper to call the callback&#39;s onRequestData, which catches any
<a name="line1191"></a> * exception and cleans up the request.
<a name="line1192"></a> * @param {string} data The request data.
<a name="line1193"></a> * @private
<a name="line1194"></a> */
<a name="line1195"></a>goog.net.ChannelRequest.prototype.safeOnRequestData_ = function(data) {
<a name="line1196"></a>  /** @preserveTry */
<a name="line1197"></a>  try {
<a name="line1198"></a>    this.channel_.onRequestData(this, data);
<a name="line1199"></a>  } catch (e) {
<a name="line1200"></a>    // Dump debug info, but keep going without closing the channel.
<a name="line1201"></a>    this.channelDebug_.dumpException(
<a name="line1202"></a>        e, &#39;Error in httprequest callback&#39;);
<a name="line1203"></a>  }
<a name="line1204"></a>};
</pre>


</body>
</html>
